package com.xunqi.permission.controller;

import com.xunqi.permission.common.JsonData;
import com.xunqi.permission.dto.DeptLevelDto;
import com.xunqi.permission.param.DeptParam;
import com.xunqi.permission.service.SysDeptService;
import com.xunqi.permission.service.SysTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "部门控制器")
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private SysTreeService sysTreeService;

    @ApiOperation(value = "跳转到部门界面",httpMethod = "GET",protocols = "HTTP",produces = "/dept.page",response = String.class)
    @RequestMapping("/dept.page")
    public String dept() {
        return "dept";
    }

    @ApiOperation(value = "部门新增功能",httpMethod = "POST",protocols = "HTTP",produces = "/save.json",response = JsonData.class)
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveDept(DeptParam param) {
        sysDeptService.save(param);
        return JsonData.success();
    }

    @ApiOperation(value = "部门树状形菜单界面的显示",httpMethod = "GET",protocols = "HTTP",produces = "/tree.json",response = JsonData.class)
    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DeptLevelDto> dtoList = sysTreeService.deptTree();
        return JsonData.success(dtoList);
    }

    @ApiOperation(value = "部门修改功能",httpMethod = "POST",protocols = "HTTP",produces = "/update.json",response = JsonData.class)
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateDept(DeptParam param) {
        sysDeptService.update(param);
        return JsonData.success();
    }

    @ApiOperation(value = "部门删除功能",httpMethod = "GET",protocols = "HTTP",produces = "/delete.json",response = JsonData.class,notes =
    "删除部门首先要判断该部门下面是否有子部门，子部门下面是否有用户，若有则提示用户无法删除，若没有则删除成功")
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") int id) {
        sysDeptService.delete(id);
        return JsonData.success();
    }
}
